#include "Test.h"
#include <iostream>
#include <stdlib.h>

Test::Test(const std::string* dirpath, size_t n)
{
	if (dirpath && n) {
		m_watcher = new SCDirWatcher(this);
		if (!m_watcher)
			goto doerr;
		while (n) {
			if (!m_watcher->add(*dirpath)) {
				std::cout << "添加监视失败：" << *dirpath << std::endl;
			} else {
				std::cout << "成功监视路径：" << *dirpath << std::endl;
			}
			--n;
			dirpath += 1;
		}
	}
doerr:
	if (n > 0) {
		std::cout << "添加监视失败：" << dirpath;
		exit(0);
	}
}

Test::~Test()
{
	if (m_watcher) {
		delete m_watcher;
		m_watcher = NULL;
	}
}

void Test::update()
{
	m_watcher->updateOnce();
}

void Test::onWatchLose(const std::string& path)
{
	std::cout << path << " 路径丢失监视。\n";
}

static void showFileChangeLog(const SCDirWatcher::FState& s, const char* funcName)
{
	std::string tmp(s.oldName);
	tmp += " 是一个文件";
	if (s.flag & SCDirWatcher::Dir) {
		tmp += "夹";
	}
	tmp += ",它发生的操作有: ";
	if (s.flag & SCDirWatcher::New)
		tmp += "创建 ";
	if (s.flag & SCDirWatcher::Del)
		tmp += "删除 ";
	if (s.flag & SCDirWatcher::Mod)
		tmp += "修改 ";
	if (s.flag & SCDirWatcher::RNa) {
		tmp += "重命名到 ";
		tmp += s.newName;
	}
	std::cout << "检测回馈来自于函数:" << funcName;
	std::cout << "\n---------------------------------------------------------\n";
	std::cout << tmp;
	std::cout << "\n---------------------------------------------------------\n";
}

void Test::onFileWatch(const SCDirWatcher::FState& s)
{
	showFileChangeLog(s, __FUNCTION__);
}

void Test::onDirWatch(const SCDirWatcher::FState& s)
{
	showFileChangeLog(s, __FUNCTION__);
}
